Relative note links - links between notes

James Peret 8 years ago
parent
commit
d15b2d3ae7

+ 9 - 5
app/scripts/controllers/note-edit-ctrl.js

@@ -10,18 +10,22 @@
10 10
 angular.module('codexApp.noteEdit', [])
11 11
   .controller('NoteEditCtrl',['$scope', '$rootScope', '$state', 'FileService', function ($scope,  $rootScope, $state, FileService) {
12 12
 
13
-    console.log('-> Note Editor opened!')
13
+
14 14
 
15 15
     $scope.note = FileService.getCurrentNote();
16 16
     $scope.container = "note-container";
17
-    $scope.raw_data = "";
17
+    $scope.raw_data = $scope.note.data;
18 18
     $scope.savedBeforeQuit = false;
19
+    console.log('-> Editing File: ' + $scope.note.path)
19 20
 
20 21
     $rootScope.$on('window-view:change', function() {
21
-      if($scope.savedBeforeQuit == false) {
22
-        $scope.savedBeforeQuit = true;
23
-        FileService.saveFile($scope.note.path, $scope.raw_data)
22
+      if($scope.raw_data != "" && $scope.raw_data != undefined) {
23
+        if($scope.savedBeforeQuit == false) {
24
+          $scope.savedBeforeQuit = true;
25
+          FileService.saveFile($scope.note.path, $scope.raw_data)
26
+        }
24 27
       }
28
+
25 29
     });
26 30
 
27 31
     $scope.aceLoaded = function(_editor) {

+ 59 - 29
app/scripts/controllers/note-view-ctrl.js

@@ -20,35 +20,65 @@ angular.module('codexApp.noteView', [])
20 20
     $scope.raw_data = "";
21 21
 
22 22
 
23
-    filesystem.readFile($scope.note.path, function(err, data) {
24
-      var str = String.fromCharCode.apply(null, data)
25
-      if(!$scope.$$phase) {
26
-        $scope.$apply(function(){
27
-          $scope.note.data = str;
28
-          $scope.raw_data = str
29
-        });
30
-      } else {
31
-          $scope.note.data = str;
32
-          $scope.raw_data = str;
33
-      }
34
-      //console.log($scope.raw_data);
35
-      var a = document.getElementsByTagName('a'), ajax;
36
-      for (var i=0; i<a.length; ++i) {
37
-         a[i].addEventListener('click', handleAnchor, false);
38
-      }
39
-      function handleAnchor(e){
40
-          e.preventDefault();
41
-          if(ajax) ajax.abort();
42
-          ajax = new XMLHttpRequest();
43
-          ajax.onload = updateContent;
44
-          ajax.open("get", this.href, true);
45
-          ajax.send();
46
-          console.log("-> Prevented link from opening: " + e.srcElement.href);
47
-      }
48
-      function updateContent() {
49
-          // Do something with `this.responseText`
50
-      }
51
-    });
23
+    $scope.loadNoteView = function() {
24
+      filesystem.readFile($scope.note.path, function(err, data) {
25
+        var str = String.fromCharCode.apply(null, data)
26
+        if(!$scope.$$phase) {
27
+          $scope.$apply(function(){
28
+            $scope.note.data = str;
29
+            $scope.raw_data = str
30
+          });
31
+        } else {
32
+            $scope.note.data = str;
33
+            $scope.raw_data = str;
34
+        }
35
+        //console.log($scope.raw_data);
36
+        var a = document.getElementsByTagName('a'), ajax;
37
+        for (var i=0; i<a.length; ++i) {
38
+           a[i].addEventListener('click', handleAnchor, false);
39
+        }
40
+        function handleAnchor(e){
41
+            e.preventDefault();
42
+            var r = new RegExp('^(?:[a-z]+:)?//', 'i');
43
+            if(e.srcElement.protocol == "http:"){
44
+              console.log("-> Prevented link from opening: " + e.srcElement.outerHTML.match(/href="([^"]*)/)[1]);
45
+            }
46
+            if(e.srcElement.protocol == "file:"){
47
+              var current_note = FileService.getCurrentNote().path;
48
+              var current_path = current_note.split('/');
49
+              current_path.pop();
50
+              var relative_path = e.srcElement.outerHTML.match(/href="([^"]*)/)[1];
51
+              relative_path = relative_path.split('/');
52
+              var count = 0;
53
+              for (var i = 0; i < relative_path.length; i++) {
54
+                if(relative_path[i] == ".."){
55
+                  count = count + 1;
56
+                  relative_path[i] = "";
57
+                }
58
+              }
59
+              relative_path = relative_path.join('/');
60
+              for (var i = 0; i < count; i++) {
61
+                current_path.pop();
62
+              }
63
+              current_path = current_path.join('/');
64
+              current_path = current_path + relative_path
65
+
66
+              var note = FileService.getNote(current_path);
67
+              FileService.setCurrentNote(note);
68
+              $scope.note = note;
69
+              console.log("-> Opening Link: " + note.path)
70
+              $scope.loadNoteView()
71
+            }
72
+
73
+
74
+        }
75
+        function updateContent() {
76
+            // Do something with `this.responseText`
77
+        }
78
+      });
79
+    }
80
+
81
+    $scope.loadNoteView();
52 82
 
53 83
     $scope.marked = function(str) {
54 84
       if(str != "" && str != undefined) {

+ 4 - 0
app/scripts/services/file-service.js

@@ -207,6 +207,10 @@ angular.module('codexApp')
207 207
     //console.log("Current_note: " + current_note.title)
208 208
   }
209 209
 
210
+  this.getNotesDir = function() {
211
+    return notes_dir;
212
+  }
213
+
210 214
   this.getDefaultNotesDir = function() {
211 215
     return default_notes_dir;
212 216
   }

+ 1 - 1
codex/Test-Stack/Notebook 2/Test-0001/index.md

@@ -5,4 +5,4 @@ Testing the codex app note structure.
5 5
 ## Some Links
6 6
 
7 7
 * [External Link](http://blog.j1x.co)
8
-* [Another note](Test-0002/index.md)
8
+* [Another note](../Test-0002/index.md)

+ 10 - 0
codex/inbox/js_absoulte_or_relative_links.md

@@ -0,0 +1,10 @@
1
+# JS absolute or relative URL
2
+
3
+    var r = new RegExp('^(?:[a-z]+:)?//', 'i');
4
+    r.test('http://example.com'); // true - regular http absolute URL
5
+    r.test('HTTP://EXAMPLE.COM'); // true - HTTP upper-case absolute URL
6
+    r.test('https://www.exmaple.com'); // true - secure http absolute URL
7
+    r.test('ftp://example.com/file.txt'); // true - file transfer absolute URL
8
+    r.test('//cdn.example.com/lib.js'); // true - protocol-relative absolute URL
9
+    r.test('/myfolder/test.txt'); // false - relative URL
10
+    r.test('test'); // false - also relative URL

+ 21 - 0
codex/inbox/test_note.md

@@ -0,0 +1,21 @@
1
+# Markdown Syntax
2
+
3
+This is a quick review of the markdown syntax.
4
+
5
+## titles
6
+
7
+    # H1
8
+    ## H2
9
+    ### H3
10
+    #### H4
11
+    ##### H5
12
+    
13
+## Lists
14
+
15
+    * Unordered list item 1
16
+    * Unordered list item 2
17
+    * Unordered list item 3
18
+    
19
+    1. Ordered list item
20
+    2. Ordered list item
21
+    3. Ordered list item